package com.xls.box2023.login;


import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.xls.box2023.R;
import com.xls.box2023.customcontrol.LoginDialog;
import com.xls.box2023.mvp.MVPBaseActivity;

import androidx.annotation.Nullable;

/**
 * MVPPlugin
 */

public class LoginActivity extends MVPBaseActivity<LoginContract.View, LoginPresenter> implements LoginContract.View, View.OnClickListener {
    public static LoginActivity loginActivity;
    ImageView login_icon;
    TextView login_btn;
    ImageView login_bg;

    public static void startActivity(Context activity) {
        Intent intent=new Intent(activity,LoginActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseApplication.getAppManager().finishOtherActivity(LoginActivity.class);
        mPresenter.regToWx();
        loginActivity=this;
    }

    @Override
    public int getView() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        login_icon = findViewById(R.id.login_icon);
        login_btn = findViewById(R.id.login_btn);
        login_bg=findViewById(R.id.login_bg);
        login_btn.setOnClickListener(this);
        ApplicationInfo appInfo = null;
        try {
            appInfo = this.getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            throw new RuntimeException(e);
        }
        if (appInfo!=null){
            String data = appInfo.metaData.getString("login_bg");
            if (!TextUtils.isEmpty(data)){
                int drawableId = getDrawableIdByName(this, data);
                if (drawableId!=0){
                    login_bg.setBackgroundResource(drawableId);
                }
            }
        }

    }

    public int getDrawableIdByName(Context context, String name) {
        Resources resources = context.getResources();
        final int resourceId = resources.getIdentifier(name, "mipmap", context.getPackageName());
        return resourceId;
    }

    @Override
    public void onClick(View view) {
        if (view.getId()==R.id.login_btn) {
            showDialog();
            mPresenter.getPermissions();
        }
    }

    LoginDialog loginDialog;

    public void showDialog(){
        if (loginDialog==null){
            loginDialog=new LoginDialog(this);
        }
        loginDialog.show();
    }

    public void dissmiss(){
        if (loginDialog!=null)loginDialog.dismiss();
    }
}
